// $Id: filehandlerdistributions.cpp 53 2004-10-11 14:43:22Z hansemil $
#include "filehandlerdistributions.h"
#include "distributions.h"

FileHandlerDistributions::~FileHandlerDistributions()
{
}

/**
 * Simply return a file to the node, not caring about who it is...
 *
 * @param requestor Ignored
 * @returns A pointer to a File object according to the current file popularity distribution (or null if there are no files!)
 * @test Confirm that it uses the distribution 100% correctly!
 */
File *FileHandlerDistributions::getFile(Node */*requestor*/)
{
	// Return null if there are no files in the network
	if (m_files.empty()) {
		return 0;
	}

	// Find file to download and return it
	/*
	// DEBUG: Output debug data
	unsigned int abdul = Distributions::getInstance()->filePopularity(rand());
//	cout << abdul << "\t" << RAND_MAX << endl;
	abdul /= (RAND_MAX/m_files.size());
//	cout << abdul << endl;
	File *olle = m_files[abdul];
	cout << olle->m_hash << "\t" << olle->m_size << endl;
	return olle;
	*/
	return m_files[(Distributions::getInstance()->filePopularity(rand())) / (RAND_MAX/m_files.size())];
}

/**
 * Create a file, insert it into the global file list and return it.
 *
 * @param requestor Ignored
 * @returns A pointer to the newly created File object
 */
File *FileHandlerDistributions::createFile(Node */*requestor*/)
{
	File *newFile = new File(rand(), Distributions::getInstance()->fileSize(rand()));
	m_files.push_back(newFile);
	return newFile;
}
